{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# YOLO 后处理之坐标变换\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "YOLO 后处理的坐标变换主要目的是将模型预测的坐标转换为图像上的真实坐标。\n",
    "\n",
    "1. 模型预测的坐标表示\n",
    "    YOLO 模型通常会对每个预测框输出 4 个坐标值，一般为 $(t_x, t_y, t_w, t_h)$，这些值是相对于网格单元的偏移和缩放。\n",
    "\n",
    "    - $t_x$ 和 $t_y$：预测框中心相对于所在网格单元左上角的偏移。\n",
    "    - $t_w$ 和 $t_h$：预测框的宽度和高度相对于锚框的缩放。\n",
    "2. 从网格单元偏移到特征图坐标\n",
    "    将预测的偏移和缩放转换为特征图上的坐标 $(b_x, b_y, b_w, b_h)$。\n",
    "\n",
    "    $$\n",
    "    \\begin{aligned}\n",
    "    &b_x = \\sigma(t_x) + c_x\\\\\n",
    "    &b_y = \\sigma(t_y) + c_y\\\\\n",
    "    &b_w = p_w * \\exp(t_w)\\\\\n",
    "    &b_h = p_h * \\exp(t_h)\n",
    "    \\end{aligned}\n",
    "    $$\n",
    "    其中：\n",
    "\n",
    "    - $\\sigma$ 是 Sigmoid 函数, 用于将偏移限制在 $[0, 1]$ 范围内。\n",
    "    - $c_x$ 和 $c_y$ 是当前网格单元左上角在特征图上的坐标。\n",
    "    - $p_w$ 和 $p_h$ 是预先定义的锚框的宽度和高度。\n",
    "3. 从特征图坐标到图像坐标\n",
    "    将特征图上的坐标转换为原始图像上的坐标 $(x, y, w, h)$。\n",
    "\n",
    "    $$\n",
    "    \\begin{aligned}\n",
    "    &x = b_x * \\text{stride}\\\\\n",
    "    &y = b_y * \\text{stride}\\\\\n",
    "    &w = b_w * \\text{stride}\\\\\n",
    "    &h = b_h * \\text{stride}\\\\\n",
    "    \\end{aligned}\n",
    "    $$\n",
    "    其中，`stride` 是特征图相对于原始图像的下采样倍数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
